<template>  
    <div>
        <div v-show='showGroup === "A"'>
            <div class="operation">
                <a class="btn btn-theme" @click="showAddGroupModal">添加权限组</a>
                <a class="btn btn-theme" @click="showModifyGroupModal">修改权限组</a>
                <a class="btn btn-theme" @click="showDeleteGroupModal">删除权限组</a>
            </div>
            <table class="table" id="group">
                
            </table>
               <div class="modal fade" id="authority_add_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content container-fluid">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                    <h4 class="modal-title" id="exampleModalLabel">添加权限组</h4>
                                </div>
                                <div class="modal-body row">
                                    <form id="authority_add_form">
                                        <div class="form-group1 col-md-12">
                                            <label for="authority_add_authorityname" class="control-label">权限组名</label>
                                            <input type="text" class="form-control input-sm" name="authorityname" id="authority_add_authorityname">
                                        </div>
                                        <div class="form-group1 col-md-12">
                                            <label for="authority_add_remark" class="control-label">备注</label>
                                            <input type="text" class="form-control input-sm" name="remark" id="authority_add_remark">
                                        </div>
                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                    <button type="submit" class="btn btn-primary" @click="addGroup">提交</button>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="modal fade" id="authority_modify_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content container-fluid">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                    <h4 class="modal-title" id="exampleModalLabel">编辑权限组</h4>
                                </div>
                                <div class="modal-body row">
                                    <form id="authority_modify_form">

                                        <div class="form-group1 col-md-12">
                                            <label for="authority_modify_groupname" class="control-label">权限组名</label>
                                            <input type="text" class="form-control input-sm" name="groupname" id="authority_modify_groupname">
                                        </div>
                                        <div class="form-group1 col-md-12">
                                            <label for="authority_modify_remark" class="control-label">备注</label>
                                            <input type="text" class="form-control input-sm" name="remark" id="authority_modify_remark">
                                        </div>
                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                    <button type="submit" class="btn btn-primary" @click="modifyGroup">提交</button>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="modal fade" id="authority_drop_modal">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                    <h4 class="modal-title">删除确定</h4>
                                </div>
                                <div class="modal-body">
                                    <p>您真的要删除此权限组吗？</p>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                    <button type="button" class="btn btn-primary" @click="deleteGroup">确认</button>
                                </div>
                            </div>
                        </div>
                    </div>
        </div>
        <div v-show='showGroup === "B"'>
            
                <!--<a class="btn btn-success" style="float: right;margin:5px; "><i class="fa fa-reply"></i> 返回</a>-->
            
           <ul class="nav nav-tabs" style="clear: both" role="tablist">
                <li role="presentation" class="active"><a href="#userinauthority" role="tab" data-toggle="tab">成员</a></li>
                <li role="presentation"><a href="#functionlist" role="tab" data-toggle="tab">功能列表</a></li>
                <a class="btn btn-success" style="float: right;margin:5px;" @click="back"><i class="fa fa-reply"></i> 返回</a>
           </ul>
           <div class="tab-content">
                <div role="tabpanel" class="tab-pane active" id="userinauthority">
                    <div class="operation">
                        <a class="btn btn-theme" @click="showOutUserModal">添加成员</a>
                        <a class="btn btn-theme" @click="removeUser">移除成员</a> 
                    </div>
                    <table id="user_table_inauthority" class="table"></table>
                    <div class="modal fade" id="authority_invite_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
                            <div class="modal-dialog modal-lg" role="document">
                                <div class="modal-content container-fluid">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                        <h4 class="modal-title" id="exampleModalLabel">权限组添加员工</h4>
                                    </div>
                                    <div class="modal-body row">
                                        <table id="user_table_outauthority"></table>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                        <button type="submit" class="btn btn-primary" @click="addUser">添加</button>
                                    </div>
                                </div>
                            </div>
                    </div>
                </div>
                <div role="tabpanel" class="tab-pane" id="functionlist">
                    <div class="operation">
                        <a class="btn btn-theme" @click="showOutFuncModal">添加功能</a>
                        <a class="btn btn-theme" @click="removeFunc">移除功能</a>
                    </div>
                    <table id="function_table_inauthority" class="table"></table>           
                     <div class="modal fade" id="function_add_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
                        <div class="modal-dialog modal-lg" role="document">
                            <div class="modal-content container-fluid">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                    <h4 class="modal-title" id="exampleModalLabel">添加功能</h4>
                                </div>
                                <div class="modal-body row">
                                    <table id="function_table_outauthority"></table>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                    <button type="submit" class="btn btn-primary" @click="addFunc">添加</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
           </div>
        </div>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                tableGroupData: '',
                showGroup: 'A',
                currentGroupId: ''
            }
        },
        methods: {
            back() {
                this.showGroup = 'A'
            },
            getGroupTable() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/scgroup/fetchalllist',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                    },
                    dataType: 'json',
                    crossDomain: true,
                    async: false,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        _this.tableGroupData = retdata.Table2
                    }
                });
                var operateEvents = {
                        'click a[name="group"]': function(e, value, row, index) {
                            _this.currentGroupId = row.fidno
                            _this.showGroup = 'B'
                            _this.getInUserTable()
                            _this.getInFuncTable()
                        }
                    }
                    // $('#group').bootstrapTable('destroy')
                $('#group').bootstrapTable({
                    columns: [{
                        radio: true
                    }, {
                        field: 'fidno',
                        title: '编号',
                        halign: 'center'
                    }, {
                        field: 'groupname',
                        title: '权限组名',
                        halign: 'center'
                    }, {
                        field: 'groupremark',
                        title: '备注',
                        halign: 'center'
                    }, {
                        field: 'ftitle',
                        title: '团队',
                        halign: 'center'
                    }, {
                        field: 'operate',
                        title: '操作',
                        halign: 'center',
                        formatter: '<a class="btn btn-theme btn-xs" name="group">操作</a>',
                        events: operateEvents
                    }],
                    data: _this.tableGroupData,
                    pagination: true,
                    paginationLoop: true,
                    onlyInfoPagination: false,
                    striped: true,
                    sidePagination: 'client',
                    pageNumber: 1,
                    pageSize: 10,
                    pageList: [10, 25, 50, 100],
                    smartDisplay: true,
                    search: true,
                    searchOnEnterKey: false,
                    strictSearch: false,
                    showHeader: true,
                    //showFooter: true,
                    showColumns: true,
                    showRefresh: true,
                    // showToggle: true,
                    showPaginationSwitch: true,
                    //detailView: true,
                    clickToSelect: true,
                    singleSelect: true,
                })
            },
            showAddGroupModal() {
                $('#authority_add_modal').modal('show')
            },
            showModifyGroupModal() {
                if ($('tr.selected').children().eq(1) == '') {
                    
                    this.$store.commit('showModal','请先选择要修改的权限组')
                } else {
                    $('#authority_modify_modal').modal('show')
                    var _this = this
                    $.ajax({
                        type: 'POST',
                        url: 'http://192.168.1.66:808/api/scgroup/fetchlistbyid',
                        data: {
                            'token': localStorage.token,
                            'userid': localStorage.userid,
                            'fidno': $('tr.selected').children().eq(1).text(),
                            'fteamid': localStorage.activeteamid
                        },
                        async: true,
                        dataType: 'json',
                        crossDomain: true,
                        error: function(request) {
                            this.$store.commit('showModal','连接错误')
                        },
                        success: function(retdata) {
                            if (retdata.Table1[0].message == 'ok') {
                                $('#authority_modify_groupname').val(retdata.Table2[0].groupname);
                                $('#authority_modify_remark').val(retdata.Table2[0].groupremark);
                            }
                        }
                    });
                }
            },
            showDeleteGroupModal() {
                if ($('tr.selected').children().eq(1) == '') {
                    this.$store.commit('showModal','请先选择要删除的权限组')
                  
                } else {
                    $('#authority_drop_modal').modal('show')
                }
            },
            addGroup() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/scgroup/add',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'groupname': $('#authority_add_authorityname').val(),
                        'groupremark': $('#authority_add_remark').val(),
                        'fteamid': localStorage.activeteamid
                    },
                    async: true,
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                         _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        if (retdata.Table1[0].message == 'ok') {
                            $('#authority_add_modal').modal('hide');
                            _this.getGroupTable()
                        }
                    }
                });
            },
            modifyGroup() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/scgroup/update',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'fidno': $('tr.selected').children().eq(1).text(),
                        'groupname': $('#authority_modify_groupname').val(),
                        'groupremark': $('#authority_modify_remark').val(),
                    },
                    async: true,
                    dataType: 'json',

                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        if (retdata.Table1[0].status == true) {
                            $('#authority_modify_modal').modal('hide');
                            _this.getGroupTable()
                        }
                    }
                });
            },
            deleteGroup() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/scgroup/delete',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'fidno': $('tr.selected').children().eq(1).text(),
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        if (retdata.Table1[0].status == true) {
                            $('#authority_drop_modal').modal('hide');
                            _this.getGroupTable()
                        }
                    }
                });
            },
            getInUserTable() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/user/fetchgroupuser',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'groupid': _this.currentGroupId
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        $('#user_table_inauthority').bootstrapTable('destroy')
                        $('#user_table_inauthority').bootstrapTable({
                            data: retdata.Table2,
                            columns: [{
                                checkbox: true,
                            }, {
                                field: 'userid',
                                title: '编号',
                                halign: 'center'
                            }, {
                                field: 'username',
                                title: '用户名',
                                halign: 'center'
                            }],
                            pagination: true,
                            paginationLoop: true,
                            onlyInfoPagination: false,
                            striped: true,
                            //sortOrder: 'desc',
                            sidePagination: 'client',
                            pageNumber: 1,
                            pageSize: 10,
                            pageList: [10, 25, 50, 100],
                            smartDisplay: true,
                            search: true,
                            searchOnEnterKey: false,
                            strictSearch: false,
                            showHeader: true,
                            selectItemName: 'inuser',
                            //showFooter: true,
                            showColumns: true,
                            showRefresh: true,
                            // showToggle: true,
                            showPaginationSwitch: true,
                            //detailView: true,
                            clickToSelect: true,
                            singleSelect: false,
                            checkboxHeader: true,
                            // radio: true,
                            checkbox: true,
                            //clickToSelect: true,
                        });
                    }
                });

            },
            getInFuncTable() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/scgroup/fetchgroupfunction',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'groupid': _this.currentGroupId
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        $('#function_table_inauthority').bootstrapTable('destroy')
                        $('#function_table_inauthority').bootstrapTable({
                            data: retdata.Table2,
                            columns: [{
                                checkbox: true,
                            }, {
                                field: 'fidno',
                                title: '编号',
                                halign: 'center'
                            }, {
                                field: 'fnotes',
                                title: '备注',
                                halign: 'center'
                            }, {
                                field: 'ftype',
                                title: '类型',
                                halign: 'center'
                            }, {
                                field: 'functionname',
                                title: 'url',
                                halign: 'center'
                            }],
                            pagination: true,
                            paginationLoop: true,
                            onlyInfoPagination: false,
                            striped: true,
                            //sortOrder: 'desc',
                            sidePagination: 'client',
                            pageNumber: 1,
                            pageSize: 10,
                            pageList: [10, 25, 50, 100],
                            smartDisplay: true,
                            search: true,
                            searchOnEnterKey: false,
                            strictSearch: false,
                            showHeader: true,
                            selectItemName: 'infunction',
                            //showFooter: true,
                            showColumns: true,
                            showRefresh: true,
                            // showToggle: true,
                            showPaginationSwitch: true,
                            //detailView: true,
                            clickToSelect: true,
                            singleSelect: false,
                            checkboxHeader: true,
                            // radio: true,
                            checkbox: true,
                            //clickToSelect: true,
                        });
                    }
                });
            },
            showOutUserModal() {
                var _this = this
                $('#authority_invite_modal').modal('show');
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/scgroup/fetchInviteuser',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'groupid': _this.currentGroupId
                    },
                    async: false,
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        $('#user_table_outauthority').bootstrapTable({
                            data: retdata.Table2,
                            columns: [{
                                checkbox: true,
                            }, {
                                field: 'fidno',
                                title: '员工号',
                            }, {
                                field: 'username',
                                title: '用户名'
                            }],
                            pagination: true,
                            paginationLoop: true,
                            onlyInfoPagination: false,
                            striped: true,
                            //sortOrder: 'desc',
                            sidePagination: 'client',
                            pageNumber: 1,
                            pageSize: 10,
                            pageList: [10, 25, 50, 100],
                            smartDisplay: true,
                            search: true,
                            searchOnEnterKey: false,
                            strictSearch: false,
                            showHeader: true,
                            selectItemName: 'outauthority',
                            //showFooter: true,
                            showColumns: true,
                            showRefresh: true,
                            // showToggle: true,
                            showPaginationSwitch: true,
                            //detailView: true,
                            clickToSelect: true,
                            singleSelect: false,
                            checkboxHeader: true,
                            // radio: true,
                            checkbox: true,
                            //clickToSelect: true,
                        });

                    }
                });
            },
            showOutFuncModal() {
                var _this = this
                $('#function_add_modal').modal('show')
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/scgroup/fetchinvitefunction',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'groupid': _this.currentGroupId,
                        'fteamid': localStorage.activeteamid
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        //_this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        $('#function_table_outauthority').bootstrapTable('destroy')
                        $('#function_table_outauthority').bootstrapTable({
                            data: retdata.Table2,
                            columns: [{
                                checkbox: true,
                            }, {
                                field: 'fidno',
                                title: '编号',
                                halign: 'center'
                            }, {
                                field: 'fnotes',
                                title: '功能名称',
                                halign: 'center'
                            }],
                            pagination: true,
                            paginationLoop: true,
                            onlyInfoPagination: false,
                            striped: true,
                            //sortOrder: 'desc',
                            sidePagination: 'client',
                            pageNumber: 1,
                            pageSize: 10,
                            pageList: [10, 25, 50, 100],
                            smartDisplay: true,
                            search: true,
                            searchOnEnterKey: false,
                            strictSearch: false,
                            showHeader: true,
                            selectItemName: 'outfunction',
                            //showFooter: true,
                            showColumns: true,
                            showRefresh: true,
                            // showToggle: true,
                            showPaginationSwitch: true,
                            //detailView: true,
                            clickToSelect: true,
                            singleSelect: false,
                            checkboxHeader: true,
                            // radio: true,
                            checkbox: true,
                            //clickToSelect: true,
                        });
                    }
                });
            },
            addUser() {
                var _this = this
                var ids = $('input:checkbox[name="outauthority"]:checked').map(function(index, elem) {
                    return $(elem).parent().next().text()
                }).get().join(',')
                console.log(ids)
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/scgroup/inviteuser',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'inviteuserids': ids, //要邀请的用户id
                        'groupid': _this.currentGroupId,
                        'teamid': localStorage.activeteamid //邀请到权限的id
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {},
                    success: function(retdata) {
                        $('#authority_invite_modal').modal('hide');
                        _this.getInUserTable()
                    }
                });
            },
            removeUser() {
                var _this = this
                var ids = $("input:checkbox[name='inuser']:checked").map(function(index, elem) {
                    return $(elem).parent().next().text()
                }).get().join(',');
                if (ids == '') {
                    _this.$store.commit('showModal','请选择要删除的成员')
                   
                } else {
                    $.ajax({
                        type: 'POST',
                        url: 'http://192.168.1.66:808/api/scgroup/removeusers',
                        data: {
                            'token': localStorage.token,
                            'userid': localStorage.userid,
                            'userids': ids,
                            'groupid': _this.currentGroupId
                        },
                        crossDomain: true,
                        dataType: 'json',
                        error: function(request) {
                            //_this.$store.commit('showModal','连接错误')
                        },
                        success: function(retdata) {
                            _this.getInUserTable()
                        }
                    });
                }
            },
            addFunc() {
                var _this = this
                var ids = $('input:checkbox[name="outfunction"]:checked').map(function(index, elem) {
                    return $(elem).parent().next().text()
                }).get()
                for (var i = 0; i < ids.length; i++) {
                    $.ajax({
                        type: 'POST',
                        url: 'http://192.168.1.66:808/api/scgroup/invitefunctionid',
                        data: {
                            'token': localStorage.token,
                            'userid': localStorage.userid,
                            'invitefunctionid': ids[i], //要邀请的功能ids
                            'groupid': _this.currentGroupId,
                            'teamid': localStorage.activeteamid //邀请到权限的id
                        },
                        dataType: 'json',
                        crossDomain: true,
                        error: function(request) {
                            //_this.$store.commit('showModal','连接错误')
                        },
                        success: function(retdata) {
                            $('#function_add_modal').modal('hide')
                            _this.getInFuncTable()
                        }
                    });
                }
            },
            removeFunc() {
                var _this = this
                var ids = '';
                ids = $("input:checkbox[name='infunction']:checked").map(function(index, elem) {
                    return $(elem).parent().next().text();
                }).get().join(',');

                if (ids == '') {
                    _this.$store.commit('showModal','请选择要删除的功能')
                    
                } else {
                    $.ajax({
                        type: 'POST',
                        url: 'http://192.168.1.66:808/api/scgroup/removefunctions',
                        data: {
                            'token': localStorage.token,
                            'userid': localStorage.userid,
                            'functionids': ids,
                            'groupid': _this.currentGroupId,
                        },
                        crossDomain: true,
                        dataType: 'json',
                        error: function(request) {
                            //_this.$store.commit('showModal','连接错误')
                        },
                        success: function(retdata) {
                            _this.getInFuncTable()
                        }
                    });
                }
            }
        },
        mounted() {
            this.getGroupTable()
        }
    }
</script>
<style>

</style>